package net.lsr.demo.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("menuController")
@RequestMapping("/menu/")
public class MenuController {
    List<Menu> list = new ArrayList<Menu>();
    AtomicLong count = new AtomicLong();

    // 配置了json转换器后，就直接返回对象
    // 要返回类型是字符串的话，就去掉json的转换器

    @PostConstruct
    public void init() {
        list.clear();
        
        Menu m = new Menu();
        m.setId(0L);
        m.setCode("菜单");
        m.setName("菜单");
        m.setParentID(0L);
        m.setOpen(true);
        list.add(m);
        
        for (int i = 0; i < 5; i++) {
            Menu menu = new Menu();
            long id = count.addAndGet(100);
            menu.setId(id);
            menu.setCode("菜单" + id);
            menu.setName("菜单" + id);
            menu.setParentID(0L);
            menu.setOpen(true);
            list.add(menu);
            for (int j = 1; j < 5; j++) {
                Menu child = new Menu();
                long cid = count.addAndGet(100);
                child.setId(cid);
                child.setCode("菜单" + cid);
                child.setName("菜单" + cid);
                child.setParentID(id);
                list.add(child);
            }
        }

        //count.addAndGet(100);
    }

    @RequestMapping("menuIndex.do")
    @ResponseBody
    public List<Menu> menuIndex() {
        return list;
    }

    @RequestMapping(value = "saveOrUpdateMenu.do", method = RequestMethod.POST)
    @ResponseBody
    public Menu saveOrUpdate(Menu menu) {
        List<Menu> newList = new ArrayList<Menu>();
        if (menu.getId() == null) {
            menu.setId(count.addAndGet(100));
            newList.addAll(list);
            newList.add(menu);
            System.out.println("add");
            list = newList;
        } else {
            for (Menu m : list) {
                if (m.getId().longValue() == menu.getId().longValue()) {
                    newList.addAll(list);
                    newList.remove(m);
                    newList.add(menu);
                    System.out.println("update");
                    break;
                }
            }
            list = newList;
        }
        
        return menu;
    }

    @RequestMapping(value = "showMenu.do", method = RequestMethod.GET)
    @ResponseBody
    public Menu show(Long id) {
        
        for (Menu m : list) {
            if (m.getId().longValue() == id.longValue()) {
                System.out.println("show");
               return m;
            }
        }
        
        
        return null;
    }

    @RequestMapping(value = "deleteMenu.do", method = RequestMethod.POST)
    @ResponseBody
    public String delete(Long id) {
        List<Menu> newList = new ArrayList<Menu>();
        
        for (Menu m : list) {
            if (m.getId().longValue() == id.longValue()) {
                newList.addAll(list);
                newList.remove(m);
               return "success";
            }
        }
        
        list = newList;
        return "failure";
    }
}
